package com.wealth.scsj.b_app.view.fragment.fourmodel;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gigamole.navigationtabstrip.NavigationTabStrip;
import com.wealth.scsj.b_app.R;
import com.wealth.scsj.b_app.adapter.FragmentAdapter;
import com.wealth.scsj.b_app.view.fragment.basemodel.BaseFragment;
import com.wealth.scsj.b_app.view.fragment.threestatemodel.FinishedFragment;
import com.wealth.scsj.b_app.view.fragment.threestatemodel.IngFragment;
import com.wealth.scsj.b_app.view.fragment.threestatemodel.RefundFragment;

import java.util.ArrayList;

/**
 *  待处理
 */

public class WaitFragment extends BaseFragment implements View.OnClickListener{

    private String[] titles = {"进行中", "已完成", "退款/售后"};
    private NavigationTabStrip tabStrip;
    private ViewPager mViewPager;
    private FragmentAdapter mainFragmentAdapter;
    private TextView mTvTopTittle;
    private EditText mSearchContentEt;
    private TextView mSearchTv;
    private TextView mAdvanceSearchTv;
    private PopupWindow mPopupWindow;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_wait, null);
        initView(inflate, null);
        initListener();
        initData();
        return inflate;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        intiNavigationBar(view);
        mTvTopTittle =view.findViewById(R.id.tv_top_tittle);
        mSearchContentEt =view.findViewById(R.id.search_content_et);
        mSearchTv =view.findViewById(R.id.search_tv);
        mAdvanceSearchTv =view.findViewById(R.id.advance_search_tv);
    }

    @Override
    protected void initListener() {
        mSearchTv.setOnClickListener(this);
        mAdvanceSearchTv.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mTvTopTittle.setText("订单处理");
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    private void intiNavigationBar(View view) {

        //顶部指示器
        tabStrip = view.findViewById(R.id.mediator_wait_tab);
        mViewPager = view.findViewById(R.id.wait_viewpager);
        mViewPager.setOffscreenPageLimit(3);
        ArrayList<BaseFragment> list = new ArrayList<>();

        IngFragment ingFragment = new IngFragment();
        FinishedFragment finishedFragment = new FinishedFragment();
        RefundFragment refundFragment = new RefundFragment();

        list.add(ingFragment);
        list.add(finishedFragment);
        list.add(refundFragment);

        mainFragmentAdapter = new FragmentAdapter(getHoldingActivity().getSupportFragmentManager(), list);
        mViewPager.setAdapter(mainFragmentAdapter);
        tabStrip.setViewPager(mViewPager, 0);
        tabStrip.setTitles(titles);
        tabStrip.setStripType(NavigationTabStrip.StripType.LINE);
        tabStrip.setStripGravity(NavigationTabStrip.StripGravity.BOTTOM);
        tabStrip.setTabIndex(0, true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_tv:

                break;
            case R.id.advance_search_tv:
                if(mPopupWindow!=null && mPopupWindow.isShowing()){
                    mPopupWindow.dismiss();
                }else{
                    showSearchPop();
                }

                break;
        }
    }

    /**
     * 高级搜索pop
     */
    private void showSearchPop() {

        View mlayoutPay = View.inflate(getHoldingActivity(), R.layout.advance_search_layout, null);
        mPopupWindow = new PopupWindow(mlayoutPay, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
//        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        LinearLayout trans_ll =mlayoutPay.findViewById(R.id.trans_ll);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAsDropDown(tabStrip);
//        backgroundAlpha(0.5f);

        //添加pop窗口关闭事件

        trans_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });

    }

    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {

//        DisplayMetrics dm = new DisplayMetrics();
//        getHoldingActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//        int width = dm.widthPixels;
//        int height =dm.heightPixels;
        WindowManager.LayoutParams lp = getHoldingActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
//        lp.width =width;
//        lp.height =height-dip2px(getHoldingActivity(),98);
        getHoldingActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getHoldingActivity().getWindow().setAttributes(lp);


    }
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
